package org.example.struct.array;

import com.alibaba.fastjson.JSON;

import java.util.Arrays;

/**
 * @author liyishan
 * @date 2023/7/18 10:57
 * @apiNote
 */

public class Test00088Merge {
    public static void main(String[] args) {
        int[] a = new int[]{1,2,0,0};
        int[] b = new int[]{2,3};

        test2(a,2,b,2);
        System.out.println("array:" + JSON.toJSON(a));
    }
    public static void test1(int[] nums1, int m, int[] nums2, int n) {
        for(int i = m; i<m+n;i++){
            nums1[i] = nums2[i-m];
        }
        Arrays.sort(nums1);
    }


    public static void test2(int[] nums1, int m, int[] nums2, int n) {
        int p1 = 0, p2 = 0;
        int[] sorted = new int[m + n];
        int cur;
        while (p1 < m || p2 < n) {
            if (p1 == m) {
                cur = nums2[p2++];
            } else if (p2 == n) {
                cur = nums1[p1++];
            } else if (nums1[p1] < nums2[p2]) {
                cur = nums1[p1++];
            } else {
                cur = nums2[p2++];
            }
            sorted[p1 + p2 - 1] = cur;
        }
        for (int i = 0; i <m+n; i++) {
            nums1[i] = sorted[i];
        }
    }

}
